import os
from urllib.parse import quote_plus

import redis
from dotenv import load_dotenv

load_dotenv()


# ------------------------
# Redis 基础配置
# ------------------------
REDIS_DEFAULTS = {
    'REDIS_USERNAME': '',
    'REDIS_HOST': '',
    'REDIS_PORT': '6379',
    'REDIS_PASSWORD': '',
    'REDIS_DB': '0',
    'REDIS_POOL_SIZE': '20',
    'REDIS_MAX_CONNECTIONS': '100',
    'REDIS_IDLE_TIMEOUT': '300',
    'REDIS_SOCKET_TIMEOUT': '5',
    'REDIS_CONNECT_TIMEOUT': '3',
    'REDIS_RETRY_ON_TIMEOUT': 'True',
    'REDIS_MAX_RETRIES': '3',
    'REDIS_SSL': 'False',
    'REDIS_SSL_CA_CERTS': '',
    'REDIS_SSL_CERT_REQS': 'required',
    'REDIS_SENTINEL_HOSTS': '',
    'REDIS_SENTINEL_SERVICE_NAME': '',
    'REDIS_SENTINEL_PASSWORD': '',
}

def get_redis_env(key):
    return os.environ.get(key, REDIS_DEFAULTS.get(key))

class RedisBuilder:
    """
    创建 Redis 连接
    """

    @staticmethod
    def get_redis_client():
        return redis.Redis(
            host=get_redis_env('REDIS_HOST'),
            port=get_redis_env('REDIS_PORT'),
            db=get_redis_env('REDIS_DB'),
            # username=get_redis_env('REDIS_USERNAME'),
            password=get_redis_env('REDIS_PASSWORD'),
            decode_responses=True,
            socket_timeout=5,
            socket_connect_timeout=3
        )

